package com.csis.system.domain;

import static com.csis.system.common.IStaticData.COLUMN_CATEGORY_CATEGORYID;
import static com.csis.system.common.IStaticData.COLUMN_CATEGORY_CATEGORYNAME;
import static com.csis.system.common.IStaticData.COLUMN_CATEGORY_CATEGORYPARENT;
import static com.csis.system.common.IStaticData.TABLE_CATEGORY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.csis.framework.util.ServiceLocator;
import com.csis.system.service.CategoryService;

@Entity
@Table(name = TABLE_CATEGORY)
public class Category {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = COLUMN_CATEGORY_CATEGORYID)
	private java.lang.Integer id = null;
	@Column(name = COLUMN_CATEGORY_CATEGORYNAME)
	private java.lang.String categoryName = null;
	@Column(name = COLUMN_CATEGORY_CATEGORYPARENT)
	private java.lang.Integer categoryParent = null;

	@Transient
	private Category parent;

	@Transient
	private List<Category> childs;

	public java.lang.Integer getId() {
		return id;
	}

	public void setId(java.lang.Integer categoryId) {
		this.id = categoryId;
	}

	public java.lang.String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(java.lang.String categoryName) {
		this.categoryName = categoryName;
	}

	public java.lang.Integer getCategoryParent() {
		return categoryParent;
	}

	public void setCategoryParent(java.lang.Integer categoryParent) {
		this.categoryParent = categoryParent;
	}

	/**
	 * @return the parent
	 */
	public Category getParent() {
		if (parent == null) {
			parent = ServiceLocator.getInstance().getService(CategoryService.class).load(categoryParent);
		}
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(Category parent) {
		this.parent = parent;
	}

	/**
	 * @return the childs
	 */
	public List<Category> getChilds() {
		if (childs == null) {
			childs = new ArrayList<Category>();
			childs.addAll(ServiceLocator.getInstance().getService(CategoryService.class)
					.getCategorysByParent(id));
		}
		return childs;
	}

	/**
	 * @param childs the childs to set
	 */
	public void setChilds(List<Category> childs) {
		this.childs = childs;
	}

}
